package com.ruoyi.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.EquipmentMaintenance;
import com.ruoyi.system.domain.EquipmentMaintenanceDetail;
import com.ruoyi.system.service.IEquipmentMaintenanceDetailService;
import com.ruoyi.system.service.IEquipmentMaintenanceService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 维保记录Controller
 * 
 * @author ruoyi
 * @date 2020-07-17
 */
@Controller
@RequestMapping("/system/maintenance")
public class EquipmentMaintenanceController extends BaseController
{
    private String prefix = "system/maintenance";

    @Autowired
    private IEquipmentMaintenanceService equipmentMaintenanceService;
    @Autowired
    private IEquipmentMaintenanceDetailService equipmentMaintenanceDetailService;

    @RequiresPermissions("system:maintenance:view")
    @GetMapping()
    public String maintenance()
    {
        return prefix + "/maintenance";
    }

    /**
     * 查询维保记录列表
     */
    @RequiresPermissions("system:maintenance:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EquipmentMaintenance equipmentMaintenance)
    {
        startPage();
        List<EquipmentMaintenance> list = equipmentMaintenanceService.selectEquipmentMaintenanceList(equipmentMaintenance);
        return getDataTable(list);
    }

    @GetMapping("/justlist")
    public String goMaintenanceList(String id,String uid,ModelMap mmap){
        EquipmentMaintenance e=new EquipmentMaintenance();
        e.setEquipmentInfoId(id);
        mmap.put("equit",e);
        return prefix + "/list";
    }

    /**
     * 导出维保记录列表
     */
    @RequiresPermissions("system:maintenance:export")
    @Log(title = "维保记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EquipmentMaintenance equipmentMaintenance)
    {
        List<EquipmentMaintenance> list = equipmentMaintenanceService.selectEquipmentMaintenanceList(equipmentMaintenance);
        ExcelUtil<EquipmentMaintenance> util = new ExcelUtil<EquipmentMaintenance>(EquipmentMaintenance.class);
        return util.exportExcel(list, "maintenance");
    }

    /**
     * 新增维保记录
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存维保记录
     */
    @RequiresPermissions("system:maintenance:add")
    @Log(title = "维保记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EquipmentMaintenance equipmentMaintenance)
    {
        return toAjax(equipmentMaintenanceService.insertEquipmentMaintenance(equipmentMaintenance));
    }

    /**
     * 修改维保记录
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        EquipmentMaintenance equipmentMaintenance = equipmentMaintenanceService.selectEquipmentMaintenanceById(id);
        mmap.put("equipmentMaintenance", equipmentMaintenance);
        return prefix + "/edit";
    }


    @GetMapping("/detail/{id}")
    public TableDataInfo detail(@PathVariable("id") Long id, ModelMap mmap)
    {
        startPage();
        EquipmentMaintenanceDetail detail=new EquipmentMaintenanceDetail();
        detail.setEquipmentMaintenanceId(id);
        List<EquipmentMaintenanceDetail> list = equipmentMaintenanceDetailService.selectEquipmentMaintenanceDetailList(detail);
        mmap.put("equipmentMainId", id);
        return getDataTable(list);
    }

    /**
     * 修改保存维保记录
     */
    @RequiresPermissions("system:maintenance:edit")
    @Log(title = "维保记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EquipmentMaintenance equipmentMaintenance)
    {
        return toAjax(equipmentMaintenanceService.updateEquipmentMaintenance(equipmentMaintenance));
    }

    /**
     * 删除维保记录
     */
    @RequiresPermissions("system:maintenance:remove")
    @Log(title = "维保记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(equipmentMaintenanceService.deleteEquipmentMaintenanceByIds(ids));
    }
}
